<?php
namespace app\home\controller;
use app\home\model\UserinfoModel;
use \think\Controller;
use \think\Model;
use think\Session;
use think\Request;

class Userinfo extends Controller{

    public function index(){

        $id=Session::get('USER.id');
        $this->assign('userid',$id);


            if(!empty($id)) {
                //获取用户信息

                $table = 'users';
                $condition = "id=$id";
                $model = new UserinfoModel();
                $row = $model->find($table, $condition);
                $this->assign("userInfo", $row);
                //未读消息

                $model = new UserinfoModel();
                $rows = $model->selectNotRead($id);
                $count = count($rows);
                $this->assign("notRead", $count);
                //我的未使用优惠券
                $id = Session::get('USER.id');;
                $condition = "user_id=$id";
                $model = new UserinfoModel();
                $rows = $model->selectCoupons($condition);
                $count = count($rows);
                //获取订单状态个数
                $order_user_id = Session::get('USER.id') ? trim(Session::get('USER.id')) : 0;
                $rows = $model->orderNum($order_user_id);
                $this->assign('rows', $rows);
                $this->assign("couponsCt", $count);
                return $this->fetch("index");            }
            else{
                    $this->redirect('home/Login/login');
             }
    }
    public function userInfo(){
         $id=Session::get('USER.id');;
        $table='users';
        $condition="id=$id";
        $model = new UserinfoModel();
        $row =$model->find($table,$condition);
        $this->assign("userInfo",$row);
        return $this->fetch('user_info');
    }
    //修改昵称
    public function updateName(){
        $id=Session::get('USER.id');;
        $table='users';
        $condition="id=$id";
        $model = new UserinfoModel();
        $row =$model->find($table,$condition);
        $this->assign("userInfo",$row);
        return $this->fetch('update_name');
    }
    public function saveName(){
        $id=Session::get('USER.id');
        $nickName=input('nickName');
        $data=[
            'nickname'=>$nickName
        ];
        $model = new UserinfoModel();
        $res =$model->saveUserInfo($id,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }
    //修改头像
    public function getAvatar(){

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('img');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload/userinfo');
            if($info){
                // 成功上传后 获取上传路径
                $path ="upload/userinfo/".$info->getSaveName();
                echo $path;

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }
    //保存头像url到数据库
    public function saveAvatar(){
        $id=Session::get('USER.id');
        $avatarPath=input('avatarPath');
        $data=[
            'avatar_url'=>$avatarPath
        ];
        $model=new UserinfoModel();
        $res =$model->saveUserInfo($id,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }
    //修改密码
    public function updatePassword(){
        $id=Session::get('USER.id');
        $table='users';
        $condition="id=$id";
        $model = new UserinfoModel();
        $row =$model->find($table,$condition);
        $this->assign('row',$row);
        return $this->fetch('update_password');
    }
    public function savePassword(){
        $id=Session::get('USER.id');
        $password=input('password');
        $data=[
            'user_pwd'=>md5($password)
        ];
        $model = new UserinfoModel();
        $res =$model->saveUserInfo($id,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }
    //修改性别
    public function updateSex(){

        return $this->fetch('update_sex');
    }
    public function saveSex(){
        $id=Session::get('USER.id');
        $password=input('sex');
        $data=[
            'sex'=>$password
        ];
        $model = new UserinfoModel();
        $res =$model->saveUserInfo($id,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }
    //修改电话
    public function updatePhone(){
        $id=Session::get('USER.id');;
        $table='users';
        $condition="id=$id";
        $model = new UserinfoModel();
        $row =$model->find($table,$condition);
        $this->assign("userInfo",$row);
        return $this->fetch('update_phone');
    }
    public function savePhone(){
        $id=Session::get('USER.id');
        $phone=input('phone');
        if(preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $phone)==false){
            echo 2;
        }else {
            $data = [
                'phone' => $phone
            ];
            $model = new UserinfoModel();
            $res = $model->saveUserInfo($id, $data);
            if ($res > 0) {
                echo 1;
            } else {
                echo 0;
            }
        }
    }
    //修改E-mail
    public function updateEmail(){
        $id=Session::get('USER.id');;
        $table='users';
        $condition="id=$id";
        $model = new UserinfoModel();
        $row =$model->find($table,$condition);
        $this->assign("userInfo",$row);
        return $this->fetch('update_email');
    }
    public function saveEmail(){
        $id=Session::get('USER.id');
        $email=input('email');
        if(preg_match('/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/',$email)==true){
        $data=[
            'email'=>$email
        ];
        $model = new UserinfoModel();
        $res =$model->saveUserInfo($id,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }else{
            echo 2;
        }
    }
    //消息
    public function message(){
         $id=Session::get('USER.id');;
        $data=[
            'status'=>1
        ];
        $model = new UserinfoModel();
        //读取消息后更新消息状态
        $model->updateMessage($id,$data);
        //读取所有消息
        $rows =$model->selectMessage($id);
        $this->assign("messages",$rows);
        return $this->fetch();
    }

    //查看所有优惠券
    public function coupons(){
         $id=Session::get('USER.id');;
        $model = new UserinfoModel();
        $condition="user_id=$id";
        $coupons =$model->selectCoupons($condition);
        $this->assign("coupons",$coupons);

        return $this->fetch();
    }
    //删除优惠券
    public function delCoupon(){
         $id=Session::get('USER.id');;
        $couponId=input('couponId');
        $model = new UserinfoModel();
        $condition="user_id=$id and coupon_id=$couponId";
        $res =$model->delCoupon($condition);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }

    }

    //好友
    public function friends(){
        $id=Session::get('USER.id');
        $table="friends_info";
        $condition="user_id=$id";
        $model=new UserinfoModel();
        $friends_id= $model->select($table,$condition);
        if(!empty($friends_id)) {
            foreach ($friends_id as $v) {
                $table = "users";
                $condition = "id=$v[friend_id]";
                $friends[] = $model->find($table, $condition);
            }
            $this->assign('friends',$friends);
        }
        return $this->fetch();
    }
    //搜索好友
    public function addFriends(){
        if(Request::instance()->isPost(input('friendId'))){
            $friendId=input('friendId');

            //查看用户是否存在
            if($friendId){
                $table='users';
                $condition="user_name LIKE '%$friendId%' or id = '$friendId'";
                $model=new UserinfoModel();
                $friends= $model->find($table,$condition);
                if($friends){
                    //查看是否已经有此好友
                    $id=Session::get('USER.id');
                    $table='friends_info';
                    $condition="user_id=$id";
                    $model=new UserinfoModel();
                    $fridenExist= $model->select($table,$condition);
                    $exist=[];
                    foreach($fridenExist as $v){
                        $exist[]=$v['friend_id'];
                    }
                    if(in_array($friends['id'],$exist)){
                        echo 2;
                    }else {
                        echo json_encode($friends);
                    }
                }else{
                        echo 0;
                }
            }
        }else {
            return $this->fetch('add_friends');
        }
    }
    //添加好友
    public function toAddFriend(){
        $id = input('id');
        $user_id = Session::get('USER.id');
        $table='friends_info';
        $data=[
            "user_id"=>$user_id,
            "friend_id"=>$id,
        ];
        $model=new UserinfoModel();
        $res= $model->insert($table,$data);
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
    //删除好友
    public function delFriend(){
        $id = input('friendId');
        $table='friends_info';
        $condition="friend_id=$id";
        $model=new UserinfoModel();
        $res= $model->deleteOne($table,$condition);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }

    }
    //查看好友信息
    public function friendInfo(){
        $id = input('friendId');
        $table='users';
        $condition="id=$id";
        $model=new UserinfoModel();
        $friend= $model->find($table,$condition);
        $this->assign('friend',$friend);
        return $this->fetch('friend_info');
    }
    //好友聊天
    public function chat(){
        $id = input('friendId');
        $table='users';
        $condition="id=$id";
        $model=new UserinfoModel();
        $friend= $model->find($table,$condition);
        $this->assign('friend',$friend);
        $userId=Session::get('USER.id');
        $this->assign('chatId',$id);
        $this->assign('userId',$userId);
        return $this->fetch();
    }
    //显示聊天内容
    public function showChat(){
        $id = Session::get('USER.id');
        $friendId = input('friendId');
        $table='reply_messages';
        $condition="user_id=$id and chat_id=$friendId";
        $model=new UserinfoModel();
        $showUser= $model->select($table,$condition);
        $table='reply_messages';
        $condition="user_id=$friendId and chat_id=$id";
        $model=new UserinfoModel();
        $showFriend= $model->select($table,$condition);
        if($showUser||$showFriend){
            //排列合并消息
            foreach($showUser as $v){
                $showArr[]=$v;
            }
            foreach($showFriend as $x){
                $showArr[]=$x;
            }
            $arr=array_column($showArr,'release_time');
            array_multisort($arr,SORT_ASC,$showArr);
            //查看用户信息
            $table='users';
            $condition="id=$id";
            $model=new UserinfoModel();
            $user= $model->find($table,$condition);
            $this->assign('user',$user);
            //查看好友信息
            $table='users';
            $condition="id=$friendId";
            $model=new UserinfoModel();
            $friend= $model->find($table,$condition);
            $this->assign('friend',$friend);
            $this->assign('id',$id);
            $this->assign('showArr',$showArr);
            $this->assign('friendId',$friendId);
        }
         return $this->fetch('show_chat');
    }
    //显示发送页面
    public function showSend(){
        $chatId=input('friendId');
        $userId=Session::get('USER.id');
        $this->assign('chatId',$chatId);
        $this->assign('userId',$userId);

        return $this->fetch('show_send');
    }
    //将聊天记录发送到数据库
    public function addChat(){
        $reply_msg_content =input('content');
        $user_id=input('userId');
        $chat_id=input('chatId');
        $table="reply_messages";
        $data=[
            'reply_msg_content'=>$reply_msg_content,
            'user_id'=>$user_id,
            'chat_id'=>$chat_id,
            'release_time'=>time(),
        ];
        $model=new UserinfoModel();
        $res= $model->insert($table,$data);
        if($res>0){
            echo 1;
        }else{
            echo 0;
        }
    }
}